﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public static class AppConst {
    public static string ExtName = ".Unity3D";
}
public class ABBuilder {

    static List<AssetBundleBuild> maps = new List<AssetBundleBuild>();

    [MenuItem("LuaFramework/开发的时候编译资源 %#d", false, 103)]
    public static void BuildDevelopResourceQuick()
    {
        BuildAssetResource(BuildTarget.StandaloneWindows, false);
    }
    public static void BuildAssetResource(BuildTarget target, bool isPublishBuild = true, string outputPath = null)
    {
        string streamPath = Application.streamingAssetsPath;
        Debug.Log(streamPath);
        if (!Directory.Exists(streamPath))
        {
            Directory.CreateDirectory(streamPath);
        }

        //CreateAssetBySubFolders(Application.dataPath + "/Resources", "");
        CreateAssetBySubFolders(Application.dataPath + "/Res1", "");

        var manifest = BuildPipeline.BuildAssetBundles(streamPath, maps.ToArray(), BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
        var bundles = manifest.GetAllAssetBundles();

        maps.Clear();
    }
    static void CreateAssetBySubFolders(string path, string namePrefix)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(path);
        //		UnityEngine.Debug.Log ("dirInfo:" + dirInfo.Name);

        if (dirInfo.Name.StartsWith("@"))
        {
            UnityEngine.Debug.Log("忽略文件夹:" + path);
            return;
        }
        // 去掉 *.asset
        const string patterns = "*.png,*.jpg,*.tga,*.mp3,*.ogg,*.wav,*.prefab,*.controller,*.fbx,*.mat,*.unity,*.shader,*.ttf,*.anim,*.psd";

        string[] files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(s => patterns.Contains(Path.GetExtension(s).ToLower())).ToArray<string>();
        if (namePrefix != "" && files.Length > 0)
        {
            //			UnityEngine.Debug.LogFormat("AddBuildMap({0}, \"\", {1});",namePrefix + AppConst.ExtName,path);
            List<string> assetFiles = new List<string>();
            var dataPath = Application.dataPath.ToLower();
            for (int i = 0; i < files.Length; i++)
            {
                var fileName = Path.GetFileNameWithoutExtension(files[i]).ToLower();
                var extention = Path.GetExtension(files[i]).ToLower();

                files[i] = files[i].ToLower();
                files[i] = files[i].Replace('\\', '/');
                files[i] = "assets" + files[i].Replace(dataPath, string.Empty);

                if (true && (
                    extention.Equals(".prefab")
                ))
                {

                    string prefabBundleName = namePrefix + "_" + fileName + AppConst.ExtName;
                    AssetBundleBuild singlePrefabBuild = new AssetBundleBuild();
                    singlePrefabBuild.assetBundleName = prefabBundleName;
                    singlePrefabBuild.assetNames = new string[] { files[i] };
                    maps.Add(singlePrefabBuild);
                }
                else
                {
                    assetFiles.Add(files[i]);
                }

            }

            string bundleName = namePrefix + AppConst.ExtName;
            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = bundleName;
            build.assetNames = assetFiles.ToArray();
            maps.Add(build);

        }

        string[] subFolders = Directory.GetDirectories(path);
        for (int i = 0; i < subFolders.Length; i++)
        {
            var folder = subFolders[i];
            folder = folder.Replace('\\', '/');
            string[] strs = folder.Split('/');
            if (strs.Length > 0)
            {
                string folerName = strs[strs.Length - 1];

                string prefix = namePrefix;
                if (namePrefix != "")
                {
                    prefix = prefix + "_";
                }
                prefix = prefix + folerName;
                CreateAssetBySubFolders(folder, prefix.ToLower());
            }
        }
        foreach (string folder in subFolders)
        {


        }
    }
}
